package com.eiss.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

import ch.qos.logback.core.util.TimeUtil;
import com.eiss.common.core.domain.AjaxResult;
import com.eiss.common.core.domain.model.LoginUser;
import com.eiss.common.utils.SecurityUtils;
import com.eiss.constants.RedisConstants;
import com.eiss.domain.DTO.TransferstationDTO;
import com.eiss.domain.Entily.Transferstation;
import com.eiss.domain.VO.TransferstationVO;
import com.eiss.mapper.TransferstationMapper;
import com.eiss.service.ITransferstationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.Pool;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.websocket.PongMessage;

/**
 * 中转站管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-24
 */
@Service
@Slf4j
public class TransferstationServiceImpl implements ITransferstationService
{
    @Autowired
    private TransferstationMapper transferstationMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询中转站管理
     * 
     * @param TransferStationID 中转站管理主键
     * @return 中转站管理
     */
    @Override
    public TransferstationDTO selectTransferstationByTransferStationID(Long TransferStationID)
    {
        return transferstationMapper.selectTransferstationByTransferStationID(TransferStationID);
    }

    /**
     * 查询中转站管理列表
     * 
     * @param transferstation 中转站管理
     * @return 中转站管理
     */
    @Override
    public List<TransferstationDTO> selectTransferstationList(Transferstation transferstation)
    {
        return transferstationMapper.selectTransferstationList(transferstation);
    }

    /**
     * 新增中转站管理
     * 
     * @param transferstationVO 中转站管理
     * @return 结果
     */
    @Override
    public int insertTransferstation(TransferstationVO transferstationVO)
    {
        Transferstation transferstation=new Transferstation();
        Transferstation build = transferstation.builder()
                .TransferStationName(transferstationVO.getTransferStationName())
                .Address(transferstationVO.getAddress())
                .City(transferstationVO.getCity())
                .Province(transferstationVO.getProvince())
                .PostalCode(transferstationVO.getPostalCode())
                .ContactPhone(transferstationVO.getContactPhone())
                .ManagerId(transferstationVO.getManagerId())
                .X(transferstationVO.getX())
                .Y(transferstationVO.getY())
                .UpdateTime(String.valueOf(LocalDateTime.now()))
                .CreateTime(String.valueOf(LocalDateTime.now())).build();
        int i = transferstationMapper.insertTransferstation(build);
        redisTemplate.opsForGeo().add(RedisConstants.REDIS_EISS_TRANSFERSTATION,new Point(Double.valueOf(build.getX()),Double.valueOf(build.getY())),build.getTransferStationID());
        return i;
    }

    /**
     * 修改中转站管理
     * 
     * @param transferstationVO 中转站管理
     * @return 结果
     */
    @Override
    public int updateTransferstation(TransferstationVO transferstationVO)
    {
        Transferstation transferstation=new Transferstation();
        Transferstation build = transferstation.builder()
                .TransferStationID(transferstationVO.getTransferStationID())
                .TransferStationName(transferstationVO.getTransferStationName())
                .Address(transferstationVO.getAddress())
                .City(transferstationVO.getCity())
                .Province(transferstationVO.getProvince())
                .PostalCode(transferstationVO.getPostalCode())
                .ContactPhone(transferstationVO.getContactPhone())
                .ManagerId(transferstationVO.getManagerId())
                .X(transferstationVO.getX())
                .Y(transferstationVO.getY())
                .UpdateTime(String.valueOf(LocalDateTime.now())).build();
        int i=transferstationMapper.updateTransferstation(build);
        redisTemplate.opsForZSet().remove(RedisConstants.REDIS_EISS_TRANSFERSTATION,build.getTransferStationID());
        redisTemplate.opsForGeo().add(RedisConstants.REDIS_EISS_TRANSFERSTATION,new Point(Double.valueOf(build.getX()),Double.valueOf(build.getY())),build.getTransferStationID());
        return i;
    }

    /**
     * 批量删除中转站管理
     * 
     * @param TransferStationIDs 需要删除的中转站管理主键
     * @return 结果
     */
    @Override
    public int deleteTransferstationByTransferStationIDs(Long[] TransferStationIDs)
    {
        for (Long TransferStationID : TransferStationIDs) {
            redisTemplate.opsForZSet().remove(RedisConstants.REDIS_EISS_TRANSFERSTATION,TransferStationID);
        }
        return transferstationMapper.deleteTransferstationByTransferStationIDs(TransferStationIDs);
    }

    /**
     * 删除中转站管理信息
     * 
     * @param TransferStationID 中转站管理主键
     * @return 结果
     */
    @Override
    public int deleteTransferstationByTransferStationID(Long TransferStationID)
    {
        redisTemplate.opsForZSet().remove(RedisConstants.REDIS_EISS_TRANSFERSTATION,TransferStationID);
        return transferstationMapper.deleteTransferstationByTransferStationID(TransferStationID);
    }

    @Override
    @Transactional
    public AjaxResult Nearbystation(Double x, Double y) {
        return AjaxResult.success();
    }
}
